/**
 * @(#) NoticeProxy.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 공지사항 조회 - Proxy
 * 				공지사항 조회하는 Model Logic
 */
package com.skt.common_hr.allapp.model
{
	import com.skt.common_hr.allapp.AllappFacade;
	import com.skt.common_hr.allapp.model.vo.NoticeVO;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class NoticeViewProxy extends CommonProxy
	{
		public static const NAME:String = "NoticeProxy";

		public var noticeList:ArrayCollection;
		
		public var acT_SCODE:ArrayCollection;	// 검색옵션코드
		public var acT_GCODE:ArrayCollection;	// 구분코드
		
		public var sE_APPORGEH:String;	// 대표코드
		public var sE_APPORGTX:String;	// 대표부  --> 발령권자로 변경
		
		public var sE_APPORGEH1:String;	// 대표코드
		public var sE_APPORGTX1:String;	// 발령부서		
		
		public var rowCount:int;
		//public var vo:NoticeVO;


		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function NoticeViewProxy(data:Object=null)
		{
			super(NAME, data);
		}


		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 *
		 * @param pernr 사번
		 * @return void
		 */
		public function loadList():void
		{
			var param:Object = new Object();
			param["I_PERNR"] = Variables.myPernr;
			loadData("Z_HR_A001", param, result1, fault1);
		}

		// 조회수 증가
		public function increaseCount(index:Number):void
		{
			var param:Object = new Object();
			param["I_CONTENT_IDX"]		= index.toString();
			trace(index);
			loadData("Z_HR_A004", param, countResult, countFault);
		}

				
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 *
		 * @param target: 1:제목,2:작성자이름,3:내용
		 * @return void
		 */
		public function keywordSearch(target:String, keyWord:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"]	= Variables.myPernr;
			param["I_RA_SEARCH"] = target;
			param["I_SEARCH"]	= keyWord;
			//param["I_MODE"]		= "M"; //관리자

			loadData("Z_HR_A002", param, result1, fault1);
		}


		//삭제
		public function deleteData(paramVo:Object):void
		{
			var tables:Object = new Object();
			var table1:Object = new Object();
			table1.CONTENT_IDX = paramVo.CONTENT_IDX;
												
			var table:Array = new Array();
			table.push(table1);			
			
			tables["T_OUT"] = table;

			
			var params:Object = new Object();			
			params["I_PERNR"]		= Variables.myPernr;
			params["I_ACTIO"]		= "DEL";
			
			saveData("Z_HR_A003", params, tables, updateResult, updateFault);
		}
/* 
		public function increaseCount(index:Number):void
		{
			var param:Object = new Object();
			param["I_BOARD_ID"]		= "03";
			param["I_CONTENT_IDX"]	= index.toString();
			trace(index);
			loadData("Z_HR_0034", param, countResult, countFault);
		}
 */
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function result1(event:ResultEvent):void
		{
			if(retCode==0) {
				noticeList 	= parseNotice(event.result.T_OUT);
				acT_SCODE	= event.result.T_SCODE;
				acT_GCODE	= event.result.T_GCODE;
				sE_APPORGEH = event.result.E_APPORGEH;
				sE_APPORGTX = event.result.E_APPORGTX;
				
				sE_APPORGEH1 = event.result.E_APPORGEH1;
				sE_APPORGTX1 = event.result.E_APPORGTX1;				
				
				facade.sendNotification(AllappFacade.LOAD_NOTICE_COMPLETE, this);
			}
			else {
				facade.sendNotification(AllappFacade.LOAD_NOTICE_FAILED, this);
			}
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function fault1(event:FaultEvent):void
		{
			facade.sendNotification(AllappFacade.LOAD_NOTICE_FAILED, this);
		}


		public function updateResult(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(AllappFacade.UPDATE_NOTICE_COMPLETE, this);
			}
			else {
				facade.sendNotification(AllappFacade.UPDATE_NOTICE_COMPLETE, this);
			}
		}


		private function updateFault(event:FaultEvent):void
		{
			facade.sendNotification(AllappFacade.UPDATE_NOTICE_FAILED, this);
		}

		public function countResult(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(AllappFacade.UPDATE_COUNT_COMPLETE, this);
			}
			else {
				facade.sendNotification(AllappFacade.UPDATE_COUNT_FAILED, this);
			}
		}


		private function countFault(event:FaultEvent):void
		{
			facade.sendNotification(AllappFacade.UPDATE_COUNT_FAILED, this);
		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////

		private function parseNotice(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			if (recArr.length == 0) {
				rowCount = 1;				
			} else {
				rowCount = recArr.length;
			} 
			
			
			for each(var obj:Object in recArr){
				var retObj:NoticeVO = new NoticeVO();
				for(var field:String in obj) {					
					if(field == "BEGDA" || field == "APPDT" || field == "CREATE_DATE" 
					|| field == "ENDDA") {					
						retObj[field+'_DT'] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DT']);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DT']);
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}		
					else if(field == "DEL_ID" || field == "DEL_TIME" || field == "DEL_FLAG" 
							|| field == "DEL_DATE" || field == "CREATE_TIME" || field == "C_LENG"
							) 
					{
					}			
					else {
						try {
							retObj[field] = obj[field];	
						}catch(e:Error) {
							trace("field : " + field + ", value : " + obj[field]);
						}
						
					}					
				}
				retArr.addItem(retObj);				
			}
			return retArr;
		}
		
		
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 공지사항 Value Object
		 */
		 private function parseNoticeBack(src:ArrayCollection):ArrayCollection
		{
			var noticeList:ArrayCollection = new ArrayCollection;
			for(var i:int = 0; i < src.length; i++)
			{
				var vo:NoticeVO = new NoticeVO();

				vo.BOARD_ID		= src[i].BOARD_ID;			//게시판 유형(번호범위 일련번호)
				vo.CONTENT_IDX	= src[i].CONTENT_IDX;		//게시물 일련번호
				vo.SEQ			= src[i].SEQ;				//게시판 유형(번호범위 일련번호)
				vo.APPDT		= DateUtil.stringToDateTime(src[i].APPDT);	//발령일자
				vo.BEGDA		= DateUtil.stringToDateTime(src[i].BEGDA);				//시작일
				vo.ENDDA		= DateUtil.stringToDateTime(src[i].ENDDA);				//종료일
										
				vo.APPGBNT		= src[i].APPGBNT;			//구분TXT
				vo.TITLE		= src[i].TITLE;				//제목
				vo.APPORGTX		= src[i].APPORGTX;			//발령부서
				vo.WRITERNM		= src[i].WRITERNM;			//작성자
				vo.CONTENT		= src[i].CONTENT;			//내용
				vo.CREATE_DATE	= DateUtil.stringToDateTime(src[i].CREATE_DATE);		//게시일자(수정일자)
								
				/* 
				vo.CONTENT		= src[i].CONTENT;			//내용				
				vo.WRITER_NAME	= src[i].WRITER_NAME;		//사원 또는 지원자의 포맷된 이름
				vo.READNUM		= src[i].READNUM;			//조회수
				vo.FILE_NAME	= src[i].FILE_NAME;			//파일이름
				vo.FILE_URL		= src[i].FILE_URL;
				trace("filename = " + vo.FILE_NAME);
				vo.bAttatched = (vo.FILE_NAME!=null) ? true : false;
				 */
				noticeList.addItem(vo);
			}
			rowCount = (i==0)?1:i;

			return noticeList;
		}
	}
}